package proxies.classes.files
{
	import core.events.PutIoErrorEvent;
	import core.events.PutIoResultEvent;
	
	import flash.net.sendToURL;
	
	import proxies.Proxy;
	import proxies.classes.files.events.FilesProxyError;
	import proxies.classes.files.events.FilesProxyListResponse;

	public class FilesProxy extends Proxy implements IFilesProxy
	{
		
		/* ------------------------ Constants -------------------------------*/
		
		public static const NAME:String = "files";
		
		public static const METHOD_LIST:String = "list";
		public static const METHOD_CREATE_DIR:String = "create_dir";
		public static const METHOD_INFO:String = "info";
		public static const METHOD_RENAME:String = "rename";
		public static const METHOD_MOVE:String = "move";
		public static const METHOD_DELETEITEM:String = "delete";
		public static const METHOD_SEARCH:String = "search";
		public static const METHOD_DIRMAP:String = "dirmap";
		
		public static const ORDERBY_ID_ASC:String = "id_asc";
		public static const ORDERBY_ID_DESC:String = "id:desc";
		public static const ORDERBY_TYPE_ASC:String = "type_asc";
		public static const ORDERBY_TYPE_DESC:String = "type_desc";
		public static const ORDERBY_NAME_ASC:String = "name_asc";
		public static const ORDERBY_NAME_DESC:String = "name_asc";
		public static const ORDERBY_EXTENSION_ASC:String = "extension_asc";
		public static const ORDERBY_EXTENSION_DESC:String = "extension_desc";
		public static const ORDERBY_CREATEDAT_ASC:String = "createdat_asc";
		public static const ORDERBY_CREATEDAT_DESC:String = "createdat_desc";
		
		public static const ITEM_TYPE_FOLDER:int = 0;
		public static const ITEM_TYPE_FILE:int = 1;
		public static const ITEM_TYPE_AUDIO:int = 2;
		public static const ITEM_TYPE_MOVIE:int = 3;
		public static const ITEM_TYPE_IMAGE:int = 4;
		public static const ITEM_TYPE_COMPRESSED:int = 5;
		public static const ITEM_TYPE_PDF:int = 6;
		public static const ITEM_TYPE_MS_DOC:int = 7;
		public static const ITEM_TYPE_TEXT:int = 8;
		public static const ITEM_TYPE_SWF:int = 9;
		
		public function FilesProxy()
		{
			super(NAME);
		}
		
		public function listFiles(id:int, parentId:int, type:String, orderby:String, offset:int=0, limit:int=20):void
		{
			var parameters:Object = new Object();
			parameters.id = id;
			parameters.parent_id = parentId;
			parameters.type = type;
			parameters.orderby = orderby;
			parameters.offset = offset;
			parameters.limit = limit;
			
			sendRequest(METHOD_LIST,onResult, onError, parameters);
		}
		
		public function createDirFiles(name:String, parentId:int):void
		{
			var parameters:Object = new Object();
			parameters.name = name;
			parameters.parent_id = parentId;
			
			sendRequest(METHOD_CREATE_DIR, onResult, onError, parameters);
		}
		
		public function infoFiles(id:int):void
		{
			var parameters:Object = new Object();
			parameters.id = id;
			
			sendRequest(METHOD_INFO, onResult, onError, parameters);
		}
		
		public function renameFile(id:int, name:String):void
		{
			var parameters:Object = new Object();
			parameters.id = id;
			parameters.name = name;
			
			sendRequest(METHOD_RENAME, onResult, onError, parameters);
		}
		
		public function moveFile(id:int, parentId:int):void
		{
			var parameters:Object = new Object();
			parameters.id = id;
			parameters.parent_id = parentId;
			
			sendRequest(METHOD_MOVE, onResult, onError, parameters);
		}
		
		public function searchFile(query:String):void
		{
			var parameters:Object = new Object();
			parameters.query = query;
			
			sendRequest(METHOD_SEARCH, onResult, onError, parameters);		
		}
		
		public function deleteFile(id:int):void
		{
			var parameters:Object = new Object();
			parameters.id = id;
			
			sendRequest(METHOD_DELETEITEM, onDeleteFileResult, onError, parameters);
		}
		
		/**
		 * Gives the directory list in raw format.
		 */
		public function dirmapFile():void
		{
			sendRequest(METHOD_DIRMAP, onDirmapResult, onError);
		}

		/* -------------------------- Tools ---------------------------------*/
		
		/**
		 * Unpack the data from the event and return a data Array filled with FilesProxyItems.
		 */
		private function buildItemList(result:Object):Array
		{
			var itemsList:Array = new Array();
			var currentItem:FilesProxyItem;
			
			for(var i:int=0; i < result.nbResults; i++)
			{		
				currentItem = new FilesProxyItem(result.results[i]);
				itemsList.push(currentItem);
			}
			
			return itemsList;
		}
		
		/* ------------------------- Listeners -------------------------------*/
		
		private function onError(error:PutIoErrorEvent):void
		{	
			//Add error message
			var filesProxyErrorEvent:FilesProxyError = new FilesProxyError(error.type, error.errorMessage, error.bubbles, error.cancelable);			
			//Fire event
			dispatchEvent(filesProxyErrorEvent);			
		}
		
		private function onResult(result:PutIoResultEvent):void
		{
			// Fetch Data into an Arraylist.
			var itemsList:Array = buildItemList(result.results);
			
			// Put them into another Event and dispatch it.
			var filesProxyListResponse:FilesProxyListResponse = new FilesProxyListResponse(itemsList, result.bubbles, result.cancelable);
			dispatchEvent(filesProxyListResponse);
		}
		
		private function onDeleteFileResult(result:PutIoResultEvent):void
		{
			// Send a confirmation that the folder has been successfully deleted (optional).
		}
		
		private function onDirmapResult(result:PutIoResultEvent):void
		{
			//TODO : Implement a recursive function or similar (depends on the depth of the arborescence).
			
		}
	}
}